package com.tistory.login.controller;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.tistory.admin.adminmgr.auth.model.AdminAuthSession;
import com.tistory.admin.adminmgr.auth.model.AdminMember;
import com.tistory.admin.common.Constants;
import com.tistory.admin.common.SessionManager;
import com.tistory.login.service.LoginService;

@Controller
public class LoginController{
	
	private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginService loginService;
	
	/*public void setHelloService(HelloService helloService) {
		this.helloService = helloService;
	}*/
	
	@RequestMapping(value="login/login.do")
	public ModelAndView login() throws SQLException{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("login/login");
		return modelAndView;
	}	
	
	@RequestMapping(value="/login/adminLoginExcute.do" , method=RequestMethod.POST)
	public ModelAndView adminLoginExcute(@RequestParam Map<String,Object> map , HttpSession session,HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
		ModelAndView modelAndView = new ModelAndView();
		 
		log.info("2342342342");
		Integer count = loginService.getUser(map);
		String returnUrl = "";
		if(count>0){  // 로그인 성공시
			returnUrl = "layout/main";
			
			SessionManager.setSession(request,Constants.ADMIN_AUTH_SESSION_KEY,Constants.YES);
			AdminAuthSession adminAuthSession = new AdminAuthSession();
			AdminMember adminMember = new AdminMember();
			
			adminMember.setId(map.get("id").toString());
			adminMember.setPassword(map.get("password").toString());
			
			adminAuthSession.setAdminMember(adminMember);
			
			session.setAttribute("id", map.get("id"));
			session.setAttribute("pw", map.get("password"));
			
			System.out.println("++++++++++++++1111+++++"+session.getAttribute("id"));
			modelAndView.setViewName(returnUrl);

			return modelAndView;
		}else{ // 로그인 실패시
			return new ModelAndView("redirect:/login/login.do?resultCd=ERROR");
		}
		
		
	}		
	
	@RequestMapping("/redirect")
	public String redirect(@RequestParam("url") String url) {
	    return "redirect:" + url;
	}
	
	private static Object getSession(HttpServletRequest request, String sessionKey) {
		if (isInvalidSession(request))
			return null;
		return request.getSession(false).getAttribute(sessionKey);
	}

	private static boolean isInvalidSession(HttpServletRequest req) {
		return (req == null || req.getSession(false) == null);
	}
	
}
